
const { lackId } = require('../constant/friendtype')
const { isFriend, WillFriend } = require('../services/FriendService')
const friendIdVaild = async (ctx, next) => {
    try {
        const { friendId = '' } = ctx.request.body
        const { user_id } = ctx.state.user
        console.log(ctx.state.user);

        if (friendId == user_id) {
            return ctx.app.emit('error', lackId, ctx)
        }
        if (!friendId) {
            return ctx.app.emit('error', lackId, ctx)
        }
        // await next()
    } catch (error) {
        console.log(error);
    }

}
const paramsVaild = async (ctx, next) => {
    try {
        const { pageNo, pageSize } = ctx.request.query

        if (!pageNo || !pageSize) {
            return ctx.app.emit('error', lackId, ctx)
        }
        await next()
    } catch (error) {
        console.log(error);
    }
}

//防止生成多余的好友关系
const preventFriend = async (ctx, next) => {
    const { friendId = '' } = ctx.request.body
    const { user_id } = ctx.state.user
    const Friend = await isFriend(friendId, user_id)
    console.log(Friend.length);

    if (Friend.length > 0) {
        return ctx.body = {
            code: 200,
            message: 'waring',
            result: "已经存在对应关系,请勿乱调用接口"
        }
    }
    await next()
}
//阻止成为Friend
const preventagree = async (ctx, next) => {
    //先查询是否存在结果再进行下一步
    const { friendId = '' } = ctx.request.body
    const { user_id } = ctx.state.user
    console.log(friendId, user_id);
    const res = await isFriend(user_id, friendId)
    console.log(res);
    if (res.length > 0) {
        await next()
        return
    }
    ctx.body = {
        code: 0,
        message: "fail",
        result: "不存在对应关系"
    }

}
module.exports = { friendIdVaild, preventFriend, preventagree, paramsVaild }